package com.andamios.sesamo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import utils.CustomResponse;

import com.andamios.sesamo.model.ClienteDTO;
import com.andamios.sesamo.model.ContratoDeServicio;
import com.andamios.sesamo.model.EncuestaDTO;
import com.andamios.sesamo.model.EncuestasCotizacion;
import com.andamios.sesamo.model.EstadoDTO;
import com.andamios.sesamo.model.GridEntity;
import com.andamios.sesamo.model.MedioEnteroDTO;
import com.andamios.sesamo.model.PerfilDTO;
import com.andamios.sesamo.servicesimpl.SesamoServicesImpl;

@Controller
public class SesamoController {
	
	 @Autowired
	    private SesamoServicesImpl sesamoServices;
	 
	 
	 
		
		@RequestMapping("home.do")
	    public ModelAndView home(HttpServletRequest request, HttpServletResponse response, ModelMap modelo) {
	        @SuppressWarnings("unused")
			HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	        //int cont = autosServices.GeneralistaValido(gpid);
	         sesamoServices.getCatalogo();
	        //return "catalogoEmpleados";
	        mav.setViewName("home");
	        return mav;
	    }
	 
	
	@RequestMapping("login.do")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")ContratoDeServicio contratoDeServicio,
	BindingResult result) {
        HttpSession session = request.getSession(true);
        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
        //int gpid = Integer.parseInt(userLogged.getGpid());
         ModelAndView mav = new ModelAndView();
         
        //int cont = autosServices.GeneralistaValido(gpid);
         
        //return "catalogoEmpleados";
        mav.setViewName("login");
        return mav;
    }
	
	
	
	@RequestMapping("menu.do")
    public ModelAndView menu(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")ContratoDeServicio contratoDeServicio,
	BindingResult result) {
        HttpSession session = request.getSession(true);
        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
        //int gpid = Integer.parseInt(userLogged.getGpid());
         ModelAndView mav = new ModelAndView();
         
        //int cont = autosServices.GeneralistaValido(gpid);
         
        //return "catalogoEmpleados";
        mav.setViewName("menu");
        return mav;
    }
	
	
	
	
	
	
	 
	 
	 
	 
	  @RequestMapping(value = "getComboFlujos.do", method = RequestMethod.GET)
	    public @ResponseBody
	    String getComboFlujos(HttpServletRequest request) {
		  
		  String code = "fdf";
	        return code;
	       
	    }  
	 

	  @RequestMapping("catalogoPerfil.do")
	    public ModelAndView catalogoPerfil(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")ContratoDeServicio contratoDeServicio,
		BindingResult result) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	        //int cont = autosServices.GeneralistaValido(gpid);
	         
	        //return "catalogoEmpleados";
	        mav.setViewName("catalogo_perfil");
	        return mav;
	    }
	  
	  
	  @RequestMapping(value = "getPerfilGrid.do", method = RequestMethod.GET)
	    public @ResponseBody
	    CustomResponse getPerfilGrid(HttpServletRequest request) {
		  

       
		    String sord = request.getParameter("sord") == null ? "" : request.getParameter("sord");
	        String sidx = request.getParameter("sidx") == null ? "" : request.getParameter("sidx");
	        int type = request.getParameter("type") == null ? 0 : Integer.parseInt(request.getParameter("type"));
	        int page = request.getParameter("page") == null ? 0 : Integer.parseInt(request.getParameter("page"));
	        int rows = request.getParameter("rows") == null ? 0 : Integer.parseInt(request.getParameter("rows"));
	        
	        
	        
	        //log.info("Received request to get all users sord:" + sord + " sidx:" + sidx + " type:" + type + " rows:" + rows + " page:" + page);
	        // Retrieve all users from the service
	        GridEntity grid =  sesamoServices.gridCataloPerfil(type, page, rows, sidx, sord);// new ArrayList<Generalista>();//


	        // Initialize our custom user response wrapper
	        CustomResponse response = new CustomResponse();

	        // Assign the result from the service to this response
	        response.setRows(grid.getRows());

	        // Assign the total number of records found. This is used for paging
	        response.setRecords(String.valueOf(grid.getRecords()));

	        // Since our service is just a simple service for teaching purposes
	        // We didn't really do any paging. But normally your DAOs or your persistence layer should support this
	        // Assign a dummy page
	        response.setPage(String.valueOf(page));

	        // Same. Assign a dummy total pages
	        response.setTotal(String.valueOf(grid.getTotalpages()));

	        // Return the response
	        // Spring will automatically convert our CustomUserResponse as JSON object. 
	        // This is triggered by the @ResponseBody annotation. 
	        // It knows this because the JqGrid has set the headers to accept JSON format when it made a request
	        // Spring by default uses Jackson to convert the object to JSON
	        return response;
		  
	       
	    } 
	  
	  
	  
	  @RequestMapping("cudCatalogoPerfil.do")
	    public String cudCatalogoPerfil(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")PerfilDTO perfilDTO,
		BindingResult result
		) {
	        HttpSession session = request.getSession(true);	 
	        
	        String accion = request.getParameter("accion") == null ? "" : request.getParameter("accion");
	        
	        if(accion.equals("add")){
	        	sesamoServices.savePerfil(perfilDTO);
	        }
	        
	        if(accion.equals("edit")){
	        	sesamoServices.editPerfil(perfilDTO);
	        }
	        
	        if(accion.equals("del")){
	        	sesamoServices.delPerfil(perfilDTO);
	        }
	        
	         
	          return "catalogo_estados";
	    }
	  
	  
	  
	  @RequestMapping("encuestaCliente.do")
	    public ModelAndView encuestaCliente(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")EncuestaDTO encuestaDTO,
		BindingResult result
		) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	        //int cont = autosServices.GeneralistaValido(gpid);
	         
	        //return "catalogoEmpleados";
	         List<MedioEnteroDTO> medioEnteroList=sesamoServices.getMdioEnteroList();
	         mav.addObject("medioEnteroList", medioEnteroList);
	         
	        mav.setViewName("encuesta_cliente");
	        return mav;
	    }
	  
	  
	  
	  
	  @RequestMapping("buscarDatosCliente.do")
	    public ModelAndView buscarDatosCliente(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")EncuestaDTO encuestaDTO,
		BindingResult result
		) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	        //int cont = autosServices.GeneralistaValido(gpid);
	         
	        //return "catalogoEmpleados";
	         List<MedioEnteroDTO> medioEnteroList=sesamoServices.getMdioEnteroList();
	         mav.addObject("medioEnteroList", medioEnteroList);
	         
	        mav.setViewName("buscarDatosCliente");
	        return mav;
	    }
	  
	  @RequestMapping("guardarEncuesta.do")
	    public ModelAndView guardarEncuesta(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")EncuestaDTO encuestaDTO,
		BindingResult result
		) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	       
	        sesamoServices.guardarEncuesta(encuestaDTO);
	        mav.setViewName("confirmarOperacion");
	        return mav;
	    }
	  
	  @RequestMapping("guardarEncuentaCotizacion.do")
	    public ModelAndView guardarEncuentaCotizacion(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")EncuestasCotizacion encuestaCotizacion,
		BindingResult result
		) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	        ModelAndView mav = new ModelAndView();
	        
	        if (encuestaCotizacion != null){
	        	sesamoServices.saveEncuestasCotizacion(encuestaCotizacion);
	        }
	        mav.setViewName("cotizacionRentaVenta");
	        return mav;
	    }
	  
	  @RequestMapping("andasmiosDeServicio.do")
	    public ModelAndView andasmiosDeServicio(HttpServletRequest request, HttpServletResponse response, ModelMap modelo
		) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	        mav.setViewName("andamiosDeServicio");
	        return mav;
	    }
	  
	  
	  @RequestMapping("seguimientosCotizaciones.do")
	    public ModelAndView seguimientosCotizaciones(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")EncuestaDTO encuestaDTO,
		BindingResult result
		) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	        //int cont = autosServices.GeneralistaValido(gpid);
	         
	        //return "catalogoEmpleados";
	        mav.setViewName("seguimiento_presupuestos_cotizaciones");
	        return mav;
	    }
	  
	  
	  
	  
	  
	  @RequestMapping(value = "seguimientosCotizacionesGrid.do", method = RequestMethod.GET)
	    public @ResponseBody
	    CustomResponse seguimientosCotizacionesGrid(HttpServletRequest request) {
		  

     
		    String sord = request.getParameter("sord") == null ? "" : request.getParameter("sord");
	        String sidx = request.getParameter("sidx") == null ? "" : request.getParameter("sidx");
	        int type = request.getParameter("type") == null ? 0 : Integer.parseInt(request.getParameter("type"));
	        int page = request.getParameter("page") == null ? 0 : Integer.parseInt(request.getParameter("page"));
	        int rows = request.getParameter("rows") == null ? 0 : Integer.parseInt(request.getParameter("rows"));
	        
	        
	        
	        //log.info("Received request to get all users sord:" + sord + " sidx:" + sidx + " type:" + type + " rows:" + rows + " page:" + page);
	        // Retrieve all users from the service
	        GridEntity grid =  sesamoServices.gridSeguimientoCotizaciones(type, page, rows, sidx, sord);// new ArrayList<Generalista>();//


	        // Initialize our custom user response wrapper
	        CustomResponse response = new CustomResponse();

	        // Assign the result from the service to this response
	        response.setRows(grid.getRows());

	        // Assign the total number of records found. This is used for paging
	        response.setRecords(String.valueOf(grid.getRecords()));

	        // Since our service is just a simple service for teaching purposes
	        // We didn't really do any paging. But normally your DAOs or your persistence layer should support this
	        // Assign a dummy page
	        response.setPage(String.valueOf(page));

	        // Same. Assign a dummy total pages
	        response.setTotal(String.valueOf(grid.getTotalpages()));

	        // Return the response
	        // Spring will automatically convert our CustomUserResponse as JSON object. 
	        // This is triggered by the @ResponseBody annotation. 
	        // It knows this because the JqGrid has set the headers to accept JSON format when it made a request
	        // Spring by default uses Jackson to convert the object to JSON
	        return response;
		  
	       
	    } 
	  
	  
	 
	  @RequestMapping("cotizacionRentaVenta.do")
	    public ModelAndView cotizacionRentaVenta(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")EncuestasCotizacion encuestaCotizacion,
		BindingResult result
		) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	        //int cont = autosServices.GeneralistaValido(gpid);
	         
	        //return "catalogoEmpleados";
	        mav.setViewName("cotizacionRentaVenta");
	        return mav;
	    }
	  
	@RequestMapping("enlazarContrato.do")
	public ModelAndView enlazarContrato(HttpServletRequest request, HttpServletResponse response, ModelMap modelo, 
			@ModelAttribute("command") EncuestasCotizacion encuestaCotizacion,
			BindingResult result) {
		HttpSession session = request.getSession(true);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("enlazarContrato");
		return mav;
	}
	
	@RequestMapping("contrato.do")
	public ModelAndView contrato(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,
			@ModelAttribute("command") ContratoDeServicio contratoDeServicio,
			BindingResult result) {
		HttpSession session = request.getSession(true);

		ModelAndView mav = new ModelAndView();
		mav.setViewName("contrato");
		return mav;
	}
	  
	  
	  
	  @RequestMapping("altaClientes.do")
	    public ModelAndView altaClientes(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")ClienteDTO clienteDTO,
		BindingResult result
		) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	        //int cont = autosServices.GeneralistaValido(gpid);
	         
	        //return "catalogoEmpleados";
	        mav.setViewName("altaClientes");
	        return mav;
	    }
	  
	  
	  
	  
	  
}
